package actions;

import model.EventModel;
import model.UserModel;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;

import javax.naming.NamingException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DBException;
import db.DBModel;

@WebServlet
public class ShowEventAction extends Action{

	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response)throws DBException{
		try{
			int id = Integer.parseInt(request.getParameter("id"));
			
			DBModel db=new DBModel();
			EventModel event=db.getEvent(id);

			if(event!=null){
				event.setVenueName(db.getVenue(event.getVenueId()).getName());
				ArrayList<UserModel> signedUpUsers = db.getUsersSignedUpForEvent(id);
				event.setSignedUpUsers(signedUpUsers);
				//find the role for current user (if any)
				String userState = "none";
				String username = request.getUserPrincipal().getName();
				int userId = 0;
				UserModel user=db.getUserRole(username);
				if(user!=null) {
					userId = user.getId();
					if(user.getUserrole().toLowerCase().equals("admin")) userState = "moderator";
				}
				int userStatus=db.getUserStatus(id, userId);
				if(userStatus == 1) userState = "banned";
				else if(userStatus == 2) userState = "signedUp";
				else if(userStatus==3)userState = "moderator";
				if(userState.equalsIgnoreCase("moderator")){
					ArrayList<UserModel>bannedUsers=db.getBannedUsers(event.getId());
					request.setAttribute("bannedUsers", bannedUsers);
				}
				request.setAttribute("userRole", userState);
				request.setAttribute("eventObject", event);
			}else{
				db.close();
				try {
					response.sendError(HttpServletResponse.SC_NOT_FOUND);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			db.close();
			//request.getRequestDispatcher("event.jsp?"+id).forward(request, response);	
			return mapping.findForward("success");
		}catch (Exception e){
			e.printStackTrace();
			request.setAttribute("error", e.toString());
			return mapping.findForward("fail");
		}	
		

	}

	// helper method for generating strings representing start time and deadline.
	public static String buildTimeString(Calendar cal){
		StringBuilder timeBuilder = new StringBuilder();
		int month = cal.get(Calendar.MONTH)+1; // zero indexed, thus +1;
		timeBuilder.append(month);
		timeBuilder.append("-");
		timeBuilder.append(cal.get(Calendar.DATE));
		timeBuilder.append("-");
		timeBuilder.append(cal.get(Calendar.YEAR));
		timeBuilder.append(" @ ");
		timeBuilder.append(cal.get(Calendar.HOUR_OF_DAY));
		timeBuilder.append(":");
		timeBuilder.append(cal.get(Calendar.MINUTE));
		return timeBuilder.toString();
	}
	
	// helper method for generating strings representing start time and deadline.
	public static String dateString(Calendar cal){
		StringBuilder dateBuilder = new StringBuilder();
		int month = cal.get(Calendar.MONTH)+1; // zero indexed, thus +1;
		dateBuilder.append(month);
		dateBuilder.append("/");
		dateBuilder.append(cal.get(Calendar.DATE));
		dateBuilder.append("/");
		dateBuilder.append(cal.get(Calendar.YEAR));
		return dateBuilder.toString();
	}
	
	// helper method for generating strings representing start time and deadline.
	public static String timeString(Calendar cal){
		StringBuilder timeBuilder = new StringBuilder();
		int hour = cal.get(Calendar.HOUR_OF_DAY)+1; // zero indexed, thus +1;
		timeBuilder.append(hour);
		timeBuilder.append(":");
		timeBuilder.append(cal.get(Calendar.MINUTE));
		return timeBuilder.toString();
	}



}
